package com.example.testapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;

public class BannerAcitivity extends AppCompatActivity {
    ViewPager mViewpager;
    ArrayList<Integer> imgs = new ArrayList<>();
    LinearLayout mLayout;
    ArrayList<ImageView> dots = new ArrayList<>();
    // 自动循环滚动
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            handler.sendEmptyMessageDelayed(1,2000);
            mViewpager.setCurrentItem(mViewpager.getCurrentItem() + 1);
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        mViewpager = findViewById(R.id.view_pager);
        mLayout = findViewById(R.id.layout);
        imgs.add(R.drawable.banner3);
        imgs.add(R.drawable.banner1);
        imgs.add(R.drawable.banner2);
        imgs.add(R.drawable.banner3);
        imgs.add(R.drawable.banner1);
        for (int i = 0; i<imgs.size()-2; i++){
            ImageView imageView = new ImageView(BannerAcitivity.this);
            // 设置宽高
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30,30);
            params.leftMargin = 5;
            params.rightMargin = 5;
            imageView.setLayoutParams(params);
            dots.add(imageView);
            mLayout.addView(imageView);
        }
        mViewpager.setAdapter(new MyAdapter());
        // 初始选中那个屏
        mViewpager.setCurrentItem(2);
        setDostImgs();
        // 设置滚动事件
        handler.sendEmptyMessage(1);

        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float v, int i) {

            }

            @Override
            public void onPageSelected(int i) {
                if(i == 0){
                    mViewpager.setCurrentItem(imgs.size() - 2,false);

                }
                if(i == imgs.size() -1){
                    mViewpager.setCurrentItem(1,false);

                }
                setDostImgs();

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }
    // 设置背景指示点
    public void setDostImgs(){
        for(int i = 0; i< dots.size();i++){
            if( i == mViewpager.getCurrentItem() - 1){
                // 选中时
                dots.get(i).setBackgroundResource(R.drawable.point2);
            }else{
                dots.get(i).setBackgroundResource(R.drawable.point1);
            }
        }
    }

    public class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return imgs.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view ==object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
           // super.destroyItem(container, position, object);
            container.removeView((View) object);
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            ImageView imageView = new ImageView(BannerAcitivity.this);
            imageView.setBackgroundResource(imgs.get(position));
            container.addView(imageView);
            return imageView;
        }
    }
}
